import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GetIP {
	public static void main(String[] args) {
		readFileByLines("C:\\Users\\xiaplus\\Downloads\\hosts.txt");
	}
	public static void replaceIP(String dest) {
		dest=	dest.replaceAll("[1-9](\\d{1,2})?\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))", "");
	}
	//((25[0-5])|(2[0-4]d)|(1dd)|([1-9]d)|d)(.((25[0-5])|(2[0-4]d)|(1dd)|([1-9]d)|d)){3}
	//^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$
	
	public static String getIP(String str) {
		String reg = "[1-9](\\d{1,2})?\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))" ;
		Pattern pattern = Pattern.compile(reg);
		Matcher m = pattern.matcher(str);
		boolean result = m.find();
		String targetResult = "";
		while (result) {
			String value = m.group();
			targetResult = value;
			result = m.find();
		}
		String finalValues= "";
		if(targetResult!=null&&targetResult.length()>0){
			finalValues=str.replaceAll(targetResult+"|\\s*", "");
		}
		return finalValues;
	}
	
	public static void readFileByLines(String fileName) {
		List<String> ipList = new ArrayList<String>();
		File file = new File(fileName);
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			int line = 1;
			String currentIP ="";
			while ((tempString = reader.readLine()) != null) {
				currentIP=getIP(tempString);
				if(!ipList.contains(currentIP)){
					ipList.add(currentIP);
				}
				line++;
			}
			for(String v:ipList){
				System.out.println(v);
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
	}
}
